package com.huisang.springboot.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Quarter;
import com.huisang.springboot.entity.User;
import com.huisang.springboot.service.UserService;
import com.huisang.springboot.utils.Code;
import com.huisang.springboot.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/echarts")
public class EchartsController {

    @Autowired
    private UserService userService;

    @GetMapping("/example")
    public Result<Map> get() {
        Result<Map> result = new Result<>();
        Map<String, Object> map = new HashMap<>();
        map.put("x", Arrays.asList("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"));
        map.put("y", Arrays.asList(150, 230, 224, 218, 135, 147, 260));
        result.setCode(Code.success);
        result.setMessage("获取成功");
        result.setData(map);

        return result;
    }

    @GetMapping("/members")
    public Result<List> members() {
        List<User> list = userService.list();
        Result<List> result = new Result<>();

        int q1 = 0; //第一季度人数
        int q2 = 0; //第二季度人数
        int q3 = 0;
        int q4 = 0;

        for (User user : list) {
            Date createTime = user.getCreateTime();
            Quarter quarter = DateUtil.quarterEnum(createTime);
            switch (quarter) {
                case Q1: q1 += 1; break;
                case Q2: q2 += 1; break;
                case Q3: q3 += 1; break;
                case Q4: q4 += 1; break;

            }
        }
        result.setCode(Code.success);
        result.setMessage("查询成功！");
        result.setData(Arrays.asList(q1, q2, q3, q4));
        return result;
    }

}
